package com.stratosgear.gmaptests;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beoui.geocell.GeocellUtils;
import com.beoui.geocell.model.Point;
import com.google.appengine.repackaged.org.json.JSONArray;
import com.google.appengine.repackaged.org.json.JSONException;

@SuppressWarnings("serial")
public class GetMarkers extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		double lat = Double.parseDouble(req.getParameter("lat"));
		double lon = Double.parseDouble(req.getParameter("lng"));

		// Transform it to a point
		Point p = new Point(lat, lon);

		try {

			Point p1 = new Point(37.949184926395034, 23.71602772750854);
			Point p2 = new Point(37.961239854827525, 23.70823859252929);
			Point p3 = new Point(38.03610947339901, 23.787578335189814);

			JSONArray markers = new JSONArray();

			JSONArray marker1 = new JSONArray();
			marker1.put(p1.getLat());
			marker1.put(p1.getLon());
			marker1.put(GeocellUtils.distance(p, p1));
			markers.put(marker1);

			JSONArray marker2 = new JSONArray();
			marker2.put(p2.getLat());
			marker2.put(p2.getLon());
			marker2.put(GeocellUtils.distance(p, p2));
			markers.put(marker2);

			JSONArray marker3 = new JSONArray();
			marker3.put(p3.getLat());
			marker3.put(p3.getLon());
			marker3.put(GeocellUtils.distance(p, p3));
			markers.put(marker3);

			resp.setContentType("text/plain");
			resp.getWriter().println(markers.toString(2));
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
